(define (make-interval a b)
    (cons a b)
)
(define (lower-bound z)
    (car z)
)
(define (upper-bound z)
    (cdr z)
)

(define (print-interval x)
    (newline)
    (display "[")
    (display (lower-bound x))
    (display ",")
    (display (upper-bound x))
    (display "]")
)

(define (add-interval x y)
    (make-interval 
        (+ (lower-bound x) (lower-bound y))
        (+ (upper-bound x) (upper-bound y))
    )
)
(define (mul-interval x y)
    (let (
        (p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y)))        
        )
        (make-interval 
            (min p1 p2 p3 p4)
            (max p1 p2 p3 p4)
        )
    )
)
(define (div-interval x y)
    (let (
        (lb (lower-bound y))
    )
    (if (< lb 0)
        (error "y lower-bound is < 0" y )
        (mul-interval x
            (make-interval 
                (/ 1.0 (upper-bound y))
                (/ 1.0 (lower-bound y))
            )
        )
    )
    )
)

(define (make-center-percent c p)
    (let (
            (w (/ (* c p) 100))
        )
        (make-interval 
            (- c w)
            (+ c w)
        )
    )
)

(define (part1 r1 r2)
    (div-interval 
        (mul-interval r1 r2)
        (add-interval r1 r2)
    )
)

(define (part2 r1 r2)
    (let ((one (make-interval 1 1 )))
        (div-interval 
            one
            (add-interval 
                (div-interval one r1)
                (div-interval one r2)
            )
        ) 
    )
)

(define r1 
    (make-center-percent 1000 30)
)
(define r2 
    (make-center-percent 35008 50)
)
(define r3 
    (make-center-percent 1000 2)
)
(define r4 
    (make-center-percent 35008 1)
)
(print-interval (part1 r1 r2))
(print-interval (part2 r1 r2))
(print-interval (part1 r3 r4))
(print-interval (part2 r3 r4))

(exit)



